package com.zcx.liu_zi_qi
import android.annotation.TargetApi
import android.app.AlertDialog
import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.graphics.Point
import android.os.Build
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.Button
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import java.util.*
import java.util.concurrent.CopyOnWriteArrayList
import kotlin.collections.HashMap

class GameActivity : AppCompatActivity(),Handler.Callback {
    val handle=Handler(this)
    var windowWidth=0
    var windowHeight=0
    lateinit var bt_regret:Button
    lateinit var bt_restart:Button
    lateinit var bt_exit:Button
    lateinit var tv_black:TextView
    lateinit var tv_white:TextView
    lateinit var gameView:GameUI

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val display=(getSystemService(Context.WINDOW_SERVICE) as WindowManager).defaultDisplay
        val size=Point()
        display.getSize(size)
        windowWidth=size.x
        windowHeight=size.y
        println("($windowWidth,$windowHeight)")
        init()
    }

    /**
     * 初始化
     */
    fun init(){
        gameView= GameUI(this)
        gameView.layoutParams=ViewGroup.LayoutParams(windowWidth*3/5,ViewGroup.LayoutParams.MATCH_PARENT)

        val userView=LayoutInflater.from(this).inflate(R.layout.user_ui,null)
        userView.layoutParams=ViewGroup.LayoutParams(windowWidth*2/5,ViewGroup.LayoutParams.MATCH_PARENT)

        bt_regret=userView.findViewById<Button>(R.id.bt_regret)
        bt_regret.setOnClickListener {
            synchronized(Map){
                if(!Map.history.isEmpty()){
                    Map.list.removeAll(Map.list)
                    Map.list=Map.copyList(Map.history.last().first)
                    Map.map=Map.copyMap(Map.history.last().second)

                    Map.printList(Map.history.last().first)
                    Map.printMap(Map.history.last().second)
                    Map.history.remove(Map.history.last())
                    Map.printList()
                    Map.printMap()
                }else{
                    Toast.makeText(this,"官人,不能再悔了", Toast.LENGTH_LONG).show()
                }

            }
        }
        bt_restart=userView.findViewById<Button>(R.id.bt_restart)
        bt_restart.setOnClickListener {
            val builder=AlertDialog.Builder(this)
            builder.setTitle("提示")
            builder.setMessage("是否重新开始")
            builder.setPositiveButton("确定",
                object :DialogInterface.OnClickListener{
                    override fun onClick(dialog: DialogInterface?, which: Int) {
                        gameView.exit()
                        this@GameActivity.init()
                    }

                })
            builder.setNegativeButton("取消",
                object :DialogInterface.OnClickListener{
                    override fun onClick(dialog: DialogInterface?, which: Int) {

                    }

                })
            builder.create().show()
        }
        bt_exit=userView.findViewById<Button>(R.id.bt_exit)
        bt_exit.setOnClickListener {
            val builder=AlertDialog.Builder(this)
            builder.setTitle("提示")
            builder.setMessage("是否退出游戏")
            builder.setPositiveButton("确定",
                object :DialogInterface.OnClickListener{
                    override fun onClick(dialog: DialogInterface?, which: Int) {
                        gameView.exit()
                        val intent= Intent()
                        intent.setClass(this@GameActivity,MainActivity::class.java)
                        this@GameActivity.finish()
                    }

                })
            builder.setNegativeButton("取消",
                object :DialogInterface.OnClickListener{
                    override fun onClick(dialog: DialogInterface?, which: Int) {

                    }

                })
            builder.create().show()
        }
        tv_black=userView.findViewById<TextView>(R.id.tv_black)
        tv_white=userView.findViewById<TextView>(R.id.tv_white)


        val layout=LinearLayout(this)
        layout.layoutParams=ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT)
        layout.addView(gameView)
        layout.addView(userView)
        setContentView(layout)
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    override fun handleMessage(msg: Message?): Boolean {
        when(msg?.what){
            1 ->{
                val builder=AlertDialog.Builder(this)
                builder.setTitle("提示")
                val str=when(msg.obj){ChessManType.WHITE ->"白子" else -> "黑子"}
                builder.setMessage("${str}获胜,是否开始下一局")
                builder.setPositiveButton("开始下一局",
                    object :DialogInterface.OnClickListener{
                        override fun onClick(dialog: DialogInterface?, which: Int) {
                            gameView.exit()
                            this@GameActivity.init()
                        }

                    })
                builder.setNegativeButton("退出",
                    object :DialogInterface.OnClickListener{
                        override fun onClick(dialog: DialogInterface?, which: Int) {

                        }

                    })
                builder.create().show()
            }
            2 ->{
                val map=msg.obj as HashMap<String, String>
                if(Map.gameModel==GameModel.RENJI){
                    tv_black.setText("黑子(电脑):"+map.get("blackCount"))
                }else{
                    tv_black.setText("黑子:"+map.get("blackCount"))
                }
                tv_white.setText("白子:"+map.get("whiteCount"))

                when(Map.whoChess){
                    ChessManType.WHITE ->{
                        tv_white.setBackgroundResource(R.color.btnSelect)
                        tv_black.setBackgroundResource(R.color.btnNotSelect)
                    }
                    else ->{
                        tv_black.setBackgroundResource(R.color.btnSelect)
                        tv_white.setBackgroundResource(R.color.btnNotSelect)
                    }
                }

            }
        }
        return true
    }

    override fun onBackPressed() {
        if(gameView.isExit) {
            super.onBackPressed()
        }else{
            Toast.makeText(this,"再按一次退出",Toast.LENGTH_LONG).show()
            gameView.isExit=true
        }
    }
}
